﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Configuration;

namespace DAL_MechSysWeb
{
    public abstract class DALBase
    {
        
        public string connectionString = "Data Source=PCRODRIGO\\SQLRODRIGO;Initial Catalog=MechSysDB;User ID=u_gpj;Password=u_gpj";
        //private string connectionString = ConfigurationManager.ConnectionStrings["StringConexao"].ConnectionString;    

        protected SqlDataReader ExecuteReader(string storedProssidere, params SqlParameter[] parameters)
        {
            try
            {
                SqlConnection sqlConnection = new SqlConnection(connectionString);

                sqlConnection.Open();

                using (SqlCommand sqlCommand = new SqlCommand(storedProssidere, sqlConnection))
                {
                    sqlCommand.CommandText = storedProssidere;

                    sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;

                    sqlCommand.Parameters.AddRange(parameters);

                    return sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        protected SqlDataReader ExecuteReader(string storedProssidere)
        {
            try
            {
                SqlConnection sqlConnection = new SqlConnection(connectionString);

                sqlConnection.Open();

                using (SqlCommand sqlCommand = new SqlCommand(storedProssidere, sqlConnection))
                {
                    sqlCommand.CommandText = storedProssidere;

                    sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;

                    return sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
                }
            }
            catch (SqlException)
            {
                throw;
            }
        }

        protected int ExecuteNonQuery(string storedProssidere, params SqlParameter[] parameters)
        {
            try
            {
                using (SqlConnection sqlConnection = new SqlConnection(connectionString))
                {
                    sqlConnection.Open();

                    using (SqlCommand sqlCommand = new SqlCommand(storedProssidere, sqlConnection))
                    {
                        sqlCommand.CommandText = storedProssidere;

                        sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;

                        sqlCommand.Parameters.AddRange(parameters);

                        return sqlCommand.ExecuteNonQuery();
                    }
                }
            }
            catch (SqlException)
            {
                throw;
            }
        }

        protected int ExecuteNonQuery(string storedProssidere)
        {
            try
            {
                using (SqlConnection sqlConnection = new SqlConnection(connectionString))
                {
                    sqlConnection.Open();

                    using (SqlCommand sqlCommand = new SqlCommand(storedProssidere, sqlConnection))
                    {
                        sqlCommand.CommandText = storedProssidere;

                        sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;

                        return sqlCommand.ExecuteNonQuery();
                    }
                }
            }
            catch (SqlException)
            {
                throw;
            }
        }

        protected SqlDataReader ExecuteReaderQuery(string storedProssidere, params SqlParameter[] parameters)
        {
            try
            {
                SqlConnection sqlConnection = new SqlConnection(connectionString);

                sqlConnection.Open();

                using (SqlCommand sqlCommand = new SqlCommand(storedProssidere, sqlConnection))
                {      
                    sqlCommand.Parameters.AddRange(parameters);

                    return sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
